package com.example.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.system.domain.BusSystemTableUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

/**
* @author CETC
* @description 针对表【bus_system_table_user】的数据库操作Mapper
* @createDate 2025-10-25 13:12:44
* @Entity com.example.system.domain.BusSystemTableUser
*/
public interface BusSystemTableUserMapper extends BaseMapper<BusSystemTableUser> {

    @Select("SELECT * FROM ${tableName} WHERE  uuid=#{uuid} and is_deleted='0'")
    public <T> T getDataByUUID(@Param("tableName")String tableName, @Param("uuid")String uuid);

    @Select({"<script>" +
            "   SELECT * FROM ${tableName} WHERE user_id = #{userId} and is_deleted='0' order by create_time desc" +
            "</script>"})
    public <T> IPage<T> pageData(@Param("tableName")String tableName,@Param("userId")String userId,Page<T> page);

    @Select({"<script>" +
            "SELECT * FROM ${tableName} WHERE user_id = #{userId} and is_deleted='0'" +
            "<if test='params != null and params.size() > 0'>" +
            "   <foreach collection='params' index='key' item='value' open=' AND (' separator=' AND ' close=')'>" +
            "       <choose>" +
            "           <when test='value == null'>" +
            "               ${key} IS NULL" +
            "           </when>" +
            "           <when test='value instanceof java.util.List or value instanceof java.util.ArrayList'>" +
            "               ${key} IN " +
            "               <foreach collection='value' item='item' open='(' separator=',' close=')'>" +
            "                   #{item}" +
            "               </foreach>" +
            "           </when>" +
            "           <otherwise>" +
            "               ${key} = #{value}" +
            "           </otherwise>" +
            "       </choose>" +
            "   </foreach>" +
            "</if>" +
            " ORDER BY create_time DESC" +
            "</script>"})
    public <T> IPage<T> pageData(Page page,
                                 @Param("tableName") String tableName,
                                 @Param("userId") String userId,
                                 @Param("params") Map<String, Object> params);


    @Update("UPDATE ${tableName} SET is_deleted='1',update_time = NOW() WHERE uuid = #{uuid}")
    public int deleteDataByUUID(@Param("tableName")String tableName,@Param("uuid")String uuid);


    /**
     *
     * @param page
     * @param tableName
     * @param userId
     * @param params
     * @return
     * @param <T>
     */
    @Select({"<script>" +
            "SELECT * FROM ${tableName} WHERE user_id = #{userId} and is_deleted='0'" +
            "<if test='params != null and params.size() > 0'>" +
            "   <foreach collection='params' index='key' item='value' open=' AND (' separator=' AND ' close=')'>" +
            "       <choose>" +
            "           <when test='key.toString().endsWith(\"_like\")'>" +
            "               REPLACE(${key.toString().replace(\"_like\", \"\")}, '_like', '') LIKE CONCAT('%', #{value}, '%')" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_llike\")'>" +
            "               REPLACE(${key.toString().replace(\"_llike\", \"\")}, '_llike', '') LIKE CONCAT('%', #{value})" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_rlike\")'>" +
            "               REPLACE(${key.toString().replace(\"_rlike\", \"\")}, '_rlike', '') LIKE CONCAT(#{value}, '%')" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_gt\")'>" +
            "               REPLACE(${key.toString().replace(\"_gt\", \"\")}, '_gt', '') &gt; #{value}" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_ge\")'>" +
            "               REPLACE(${key.toString().replace(\"_ge\", \"\")}, '_ge', '') &gt;= #{value}" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_lt\")'>" +
            "               REPLACE(${key.toString().replace(\"_lt\", \"\")}, '_lt', '') &lt; #{value}" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_le\")'>" +
            "               REPLACE(${key.toString().replace(\"_le\", \"\")}, '_le', '') &lt;= #{value}" +
            "           </when>" +
            "           <when test='key.toString().endsWith(\"_ne\")'>" +
            "               REPLACE(${key.toString().replace(\"_ne\", \"\")}, '_ne', '') != #{value}" +
            "           </when>" +
            "           <when test='value instanceof java.util.List or value instanceof java.util.ArrayList'>" +
            "               ${key} IN " +
            "               <foreach collection='value' item='item' open='(' separator=',' close=')'>" +
            "                   #{item}" +
            "               </foreach>" +
            "           </when>" +
            "           <when test='value == null'>" +
            "               ${key} IS NULL" +
            "           </when>" +
            "           <otherwise>" +
            "               ${key} = #{value}" +
            "           </otherwise>" +
            "       </choose>" +
            "   </foreach>" +
            "</if>" +
            "   ORDER BY create_time DESC" +
            "</script>"})
    public <T> IPage<T> pageDataParams(Page<T> page,
                                 @Param("tableName") String tableName,
                                 @Param("userId") String userId,
                                 @Param("params") Map<String, Object> params);
}




